﻿@page "/downloads"
@using Microsoft.AspNetCore.Hosting
@using Microsoft.Extensions.Logging 

@inject AuthenticationStateProvider AuthProvider
@inject IDataService DataService
@inject UserManager<RemotelyUser> UserManager
@inject IWebHostEnvironment HostEnv
@inject NavigationManager NavManager
@inject ILogger<Downloads> Logger 

<div class="row mb-3">
    <h4>Portable Instant Support Clients</h4>
    <div class="text-info col-sm-12 pl-0 mb-2">
        Instant desktop sharing.  No account required.
    </div>
    <div class="col-sm-6 mb-3">
        <strong>Windows (64-Bit)</strong>
        <p>
            <a target="_blank" href="/api/ClientDownloads/Desktop/WindowsDesktop-x64/@_organizationId">Windows EXE</a>
        </p>
    </div>
    <div class="col-sm-6">
        <strong>Windows (32-Bit)</strong>
        <p>
            <a target="_blank" href="/api/ClientDownloads/Desktop/WindowsDesktop-x86/@_organizationId">Windows EXE</a>
        </p>
    </div>
    <div class="col-sm-6 mb-3">
        <strong>Linux 64-Bit</strong>
        <p>
            <a target="_blank" href="/api/ClientDownloads/Desktop/UbuntuDesktop/@_organizationId">Ubuntu Executable</a>
        </p>
    </div>
    @*<div class="col-sm-6 mb-3">
        <strong>macOS x64 (10.12 - 10.15)</strong>
        <p>
            <a target="_blank" href="/api/ClientDownloads/Desktop/MacOS-x64/@_organizationId">macOS x64 Executable</a>
        </p>
        <strong>macOS arm64 (11.01)</strong>
        <p>
            <a target="_blank" href="/api/ClientDownloads/Desktop/MacOS-arm64/@_organizationId">macOS arm64 Executable</a>
        </p>
    </div>*@
</div>


<div class="row mb-3">
    <h4>Installable Instant Support Clients</h4>
    <div class="text-info col-sm-12 pl-0 mb-2">
        Light-weight, self-updating quick support clients.
    </div>

    @if (!_isServerUrlEmbedded)
    {
        <div class="col-sm-12">
            <AlertBanner Message="Must be built from source to target specific server URL." StatusClass="warning" />
        </div>
    }


    <div class="col-sm-6 mb-3">
        <strong>Windows (64-Bit)</strong>
        <p>
            <small>Note: Only the default organization's branding will apply to these.</small>
        </p>
        <p>
            <a target="_blank" href="/Content/Win-x64/ClickOnce/setup.exe">Windows Installer</a>
        </p>
    </div>
    <div class="col-sm-6">
        <strong>Windows (32-Bit)</strong>
        <p>
            <small>Note: Only the default organization's branding will apply to these.</small>
        </p>
        <p>
            <a target="_blank" href="/Content/Win-x86/ClickOnce/setup.exe">Windows Installer</a>
        </p>
    </div>

</div>

<div class="row">
    <h4>Resident Agents</h4>
    <div class="text-info col-sm-12 pl-0 mb-2">
        Installable background agents that provide unattended access and remote scripting.
    </div>

    @if (!_isAuthenticated)
    {
        <div class="col-sm-6 mb-3">
            <h6>Must be logged in to download.</h6>
        </div>

    }
    else
    {
        <div class="col-sm-6 mb-3">
            <strong>Windows 10 / 8.1 / 7 (64-Bit and 32-Bit)</strong>
            <p>
                <small>Note: GPU-accelerated screen capture and PowerShell Core is unavailable on Windows 7.</small>
            </p>
            <p>
                <a target="_blank" href="/API/ClientDownloads/WindowsInstaller">Windows Installer (x64/x86)</a>
                <br />
                <a target="_blank" href="/Content/Remotely-Win10-x64.zip">Windows x64 Files Only</a>
                <br />
                <a target="_blank" href="/Content/Remotely-Win10-x86.zip">Windows x86 Files Only</a>
            </p>

            <p>
                <div class="small">Example Quiet Install:</div>
                
                <code class="label label-default small">
                    Remotely_Installer.exe
                    -install
                    -quiet
                    -organizationid "0b3d706b-9c5d-41e6-8ae9-5720d16324e6"
                    -serverurl "https://remotely.mytechshop.com"
                </code>
            </p>
            <p>
                <div class="small">Example Quiet Uninstall:</div>

                <code class="label label-default small">Remotely_Installer.exe -uninstall -quiet</code>
            </p>
            <p>
                <div class="small">Example Local Install:</div>
               
                <code class="label label-default small">
                    Remotely_Installer.exe
                    -install
                    -quiet
                    -organizationid "0b3d706b-9c5d-41e6-8ae9-5720d16324e6"
                    -serverurl "https://remotely.mytechshop.com"
                    -path "[path]\Remotely-Win10-x64.zip"
                </code>
            </p>
            <p>
                <div class="small">All Override Options:</div>
                
                <code class="label label-default small">
                    Remotely_Installer.exe -install -quiet -supportshortcut
                    -organizationid "0b3d706b-9c5d-41e6-8ae9-5720d16324e6"
                    -serverurl "https://remotely.mytechshop.com"
                    -devicegroup "Customer Group 1"
                    -devicealias "Front Desk"
                    -deviceuuid "eebb4d87-5459-4976-989e-a7876dffc69c"
                </code>
            </p>
        </div>

        <div class="col-sm-6 mb-3">
            <strong>Linux 64-Bit</strong>
            <p>
                <a target="_blank" href="/API/ClientDownloads/UbuntuInstaller-x64">Ubuntu x64 Bash Installer</a>
                <br />
                <a target="_blank" href="/API/ClientDownloads/ManjaroInstaller-x64">Manjaro x64 Bash Installer</a>
                <br />
                <a target="_blank" href="/Content/Remotely-Linux.zip">Linux x64 Files Only</a>
            </p>
            <p>
                <div class="small">Example Install:</div>
                
                <code class="label label-default small">sudo [path]/Install-Ubuntu-x64.sh</code>
            </p>
            <p>
                <div class="small">Example Local Install:</div>
              
                <code class="label label-default small">sudo [path]/Install-Ubuntu-x64.sh --path [path]/Remotely-Linux.zip</code>
            </p>
            <p>
                <div class="small">Uninstall:</div>

                <code class="label label-default small">sudo [path]/Install-Ubuntu-x64.sh --uninstall</code>
            </p>
        </div>

        <div class="col-sm-6 mb-3">
            <strong>macOS x64 (10.12 - 10.15)</strong>
            <p>
                <a target="_blank" href="/API/ClientDownloads/MacOSInstaller-x64">macOS x64 Bash Installer</a>
                <br />
                <a target="_blank" href="/Content/Remotely-MacOS-x64.zip">macOS x64 Files Only</a>
            </p>

            @*<strong>macOS arm64 (11.01)</strong>
            <p>
                <a target="_blank" href="/API/ClientDownloads/MacOSInstaller-arm64">macOS arm64 Bash Installer</a>
                <br />
                <a target="_blank" href="/Content/Remotely-MacOS-arm64.zip">macOS arm64 Files Only</a>
            </p>*@

            <p>
                <div class="small">Example Install:</div>
                
                <code class="label label-default small">sudo [path]/Install-MacOS-x64.sh</code>
            </p>
            <p>
                <div class="small">Example Local Install:</div>
               
                <code class="label label-default small">sudo [path]/Install-MacOS-x64.sh --path [path]/Remotely-MacOS-x64.zip</code>
            </p>
            <p>
                <div class="small">Example Uninstall:</div>
                
                <code class="label label-default small">sudo [path]/Install-MacOS-x64.sh --uninstall</code>
            </p>
        </div>
    }

</div>

@code {
    private string _organizationId;
    private bool _isServerUrlEmbedded;
    private bool _isAuthenticated;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var authState = await AuthProvider.GetAuthenticationStateAsync();
        _isAuthenticated = authState.User.Identity.IsAuthenticated;

        if (_isAuthenticated)
        {
            var currentUser = await DataService.GetUserAsync(authState.User.Identity.Name);
            _organizationId = currentUser.OrganizationID;
        }
        else
        {
            _organizationId = (await DataService.GetDefaultOrganization())?.ID;
        }

        var appFilePath = System.IO.Path.Combine(
            HostEnv.WebRootPath,
            "Content",
            "Win-x64",
            "ClickOnce",
            "Remotely_Desktop.application");

        try
        {
            await ClickOnceMiddleware.AppFileLock.WaitAsync();
            var appContent = await System.IO.File.ReadAllTextAsync(appFilePath);
            _isServerUrlEmbedded = appContent.Contains(NavManager.BaseUri.TrimEnd('/'));
        }
        catch (Exception ex)
        {
            Logger.LogWarning(ex, "Error while checking ClickOnce file.");
        }
        finally
        {
            ClickOnceMiddleware.AppFileLock.Release();
        }
    }
}